package cn.woblog.android.common.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;

import cn.woblog.android.common.utils.Const;

/**
 * Created by ren on 2015/5/5 0005.
 */
public class DBHelper extends OrmLiteSqliteOpenHelper {

    public DBHelper(Context context) {
        super(context, Const.DB_NAME , null, Const.DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
//        try {
//            TableUtils.createTable(connectionSource, User.class);
//        } catch (SQLException e) {
//            e.printStackTrace();
////            Log.e(DataHelper.class.getName(), "创建数据库失败", e);
//        }
    }

    private String getTable(Class tClass){
        return tClass.getName();
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int i1, int i2) {
//        try {
////            TableUtils.createTableIfNotExists(connectionSource, User.class);
//
////            this.source = connectionSource;
//            db.beginTransaction();
//
//            // 1, Rename table.
//            String tableName = getTable(User.class);
//            String tempTableName =tableName + "_temp";
//            String sql = "ALTER TABLE " + tableName + " RENAME TO " + tempTableName;
//            execSQL(db, sql, null);
//
////            Logs.d(TAG, "sql " + sql);
//            // 2, Create table.
//            onCreateTable(db);
//
//            String[] columns = getColumnNames(db, tempTableName);
//            StringBuilder sBuilder = new StringBuilder();
//            for (int i = 0; i < columns.length; i++) {
//                sBuilder.append(columns[i]);
//                if (i != columns.length - 1) {
//                    sBuilder.append(",");
//                }
//            }
//            // 3, Load data
//            String columString = sBuilder.toString();
//            sql = "INSERT INTO " + tableName + " (" + columString + ") " + " SELECT " + columString + " FROM " + tempTableName;
//
////            Logs.d(TAG, "sql " + sql);
//            execSQL(db, sql, null);
//
//            // 4, Drop the temporary table.
//            String dropTable = "DROP TABLE IF EXISTS " + tempTableName;
//            execSQL(db, dropTable, null);
//
////            Logs.d(TAG, "sql " + sql);
//            db.setTransactionSuccessful();
//            db.endTransaction();
//        } catch (Exception e) {
////            Logs.d(TAG, "database upgrade error");
//            e.printStackTrace();
//        }
    }

    String[] getColumnNames(SQLiteDatabase db, String tableName) {
        String[] columnNames = null;
        Cursor c = null;

        try {
            c = db.rawQuery("PRAGMA table_info(" + tableName + ")", null);
            if (null != c) {
                int columnIndex = c.getColumnIndex("name");
                if (-1 == columnIndex) {
                    return null;
                }

                int index = 0;
                columnNames = new String[c.getCount()];
                for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
                    columnNames[index] = c.getString(columnIndex);
                    index++;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (c != null) {
                c.close();
            }
        }

        return columnNames;
    }

    private void execSQL(SQLiteDatabase db, String sql, Object[] object) {
        if (object != null) {
            db.execSQL(sql, object);
        } else {
            db.execSQL(sql);
        }
    }

    private void onCreateTable(SQLiteDatabase db) {
//        try {
//            TableUtils.createTable(connectionSource, User.class);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
    }
}
